package it.univaq.di.helpdesk.common;

import it.univaq.di.helpdesk.business.common.HelpDeskServiceFactory;
import it.univaq.di.helpdesk.business.common.impl.ibatis.IBATISHelpDeskServiceFactory;
import it.univaq.di.helpdesk.business.errors.BusinessException;
import it.univaq.di.helpdesk.common.utils.JavaMailHelper;
import it.univaq.di.helpdesk.common.utils.JavaMailSMPTData;
import it.univaq.di.helpdesk.common.utils.MenuManager;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.mail.MessagingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class HelpDeskContextListener implements ServletContextListener {
	private Log logger = LogFactory.getLog(HelpDeskContextListener.class);
	
	/**
	 * Eseguito al momento del deployment dell'applicazione
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		ServletContext servletContext = event.getServletContext();
		
		String log4jProperties = servletContext.getInitParameter("log4jproperties");
		PropertyConfigurator.configure(servletContext.getRealPath(log4jProperties));

		JavaMailSMPTData jmsd = new JavaMailSMPTData();
		jmsd.setMailSMPTHost(servletContext.getInitParameter("mailSmtpHost"));
		jmsd.setMailSender(servletContext.getInitParameter("mailSender"));
		jmsd.setMailUsername(servletContext.getInitParameter("mailSender"));
		jmsd.setMailPassword(servletContext.getInitParameter("mailPassword"));
		jmsd.setAuthEnabled(Boolean.getBoolean(servletContext.getInitParameter("smtpAuthEnabled")));
		
		String ibatisConfig = servletContext.getInitParameter("ibatisConfig");
		String resource = servletContext.getRealPath(ibatisConfig);
		InputStream reader = null;
		
		try {
			//Reader reader = Resources.getResourceAsReader (risorsa espressa in termini di classpath);
			reader = new FileInputStream(resource);
			SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
			HelpDeskServiceFactory.setInstance(new IBATISHelpDeskServiceFactory(sqlMap));
			
			MenuManager.loadMenu();
			
			JavaMailHelper.initMailer(jmsd);			
		} catch (IOException e) {
			/*System.err.println("iBATIS instantiation error!");
			System.err.println(e.getMessage());*/
			logger.error("iBATIS instantiation error!", e);
		} catch (BusinessException e) {
			/*System.err.println("Error instantiating MenuManager!");
			System.err.println(e.getMessage());*/
			logger.error("Error instantiating MenuManager!", e);
		} catch (MessagingException e) {
			/*System.err.println("Error while JavaMailHelper instantiation!");
			System.err.println(e.getMessage());*/
			logger.error("Error while JavaMailHelper instantiation!", e);
		} finally {
			if (reader!=null) {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) {		
	}
}
